<template>
  <div class="supplier-container">
    <!-- 搜索区域 -->
    <el-card class="search-card">
      <el-form :model="searchForm" label-width="100px" inline>
        <el-form-item label="供应商名称">
          <el-input v-model="searchForm.supplierName" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="联系人姓名">
          <el-input v-model="searchForm.linkName" placeholder="请输入联系人姓名" />
        </el-form-item>
        <el-form-item label="联系人手机">
          <el-input v-model="searchForm.linkPhone" placeholder="请输入联系人手机" />
        </el-form-item>
        <el-form-item label="省份">
          <el-select v-model="searchForm.provinceId" placeholder="请选择省份" clearable>
            <el-option label="北京市" :value="110000" />
            <el-option label="上海市" :value="310000" />
            <el-option label="广东省" :value="440000" />
            <el-option label="浙江省" :value="330000" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市">
          <el-select v-model="searchForm.cityCode" placeholder="请选择城市" clearable>
            <el-option label="北京市" :value="110100" />
            <el-option label="上海市" :value="310100" />
            <el-option label="广州市" :value="440100" />
            <el-option label="深圳市" :value="440300" />
            <el-option label="杭州市" :value="330100" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 操作按钮 -->
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增供应商</el-button>
    </div>

    <!-- 表格数据 -->
    <el-card>
      <el-table :data="tableData" border style="width: 100%" v-loading="loading">
        <el-table-column prop="supplierName" label="供应商名字" />
        <el-table-column prop="linkName" label="联系人名字" />
        <el-table-column prop="linkPhone" label="联系人手机号" />
        <el-table-column prop="address" label="详细地址" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页 -->
      <el-pagination
        layout="total, prev, pager, next"
        :total="total"
        :page-size="pageSize"
        :current-page="currentPage"
        @current-change="handlePageChange"
        style="margin-top: 20px; text-align: right;"
      />
    </el-card>

    <!-- 新增/编辑弹窗 -->
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="500px"
      @close="handleDialogClose"
    >
      <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="供应商名字" prop="supplierName">
          <el-input v-model="form.supplierName" placeholder="请输入供应商名字" />
        </el-form-item>
        <el-form-item label="联系人名字" prop="linkName">
          <el-input v-model="form.linkName" placeholder="请输入联系人名字" />
        </el-form-item>
        <el-form-item label="联系人手机" prop="linkPhone">
          <el-input v-model="form.linkPhone" placeholder="请输入联系人手机" />
        </el-form-item>
        <el-form-item label="省份" prop="provinceId">
          <el-select v-model="form.provinceId" placeholder="请选择省份" style="width: 100%;">
            <el-option label="北京市" :value="110000" />
            <el-option label="上海市" :value="310000" />
            <el-option label="广东省" :value="440000" />
            <el-option label="浙江省" :value="330000" />
          </el-select>
        </el-form-item>
        <el-form-item label="城市" prop="cityCode">
          <el-select v-model="form.cityCode" placeholder="请选择城市" style="width: 100%;">
            <el-option label="北京市" :value="110100" />
            <el-option label="上海市" :value="310100" />
            <el-option label="广州市" :value="440100" />
            <el-option label="深圳市" :value="440300" />
            <el-option label="杭州市" :value="330100" />
          </el-select>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" type="textarea" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 表格数据相关
const tableData = ref([])
const loading = ref(false)
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)

// 搜索表单
const searchForm = reactive({
  supplierName: '',
  linkName: '',
  linkPhone: '',
  provinceId: null,
  cityCode: null
})

// 弹窗相关
const dialogVisible = ref(false)
const dialogTitle = ref('')
const isEdit = ref(false)

// 表单数据
const form = reactive({
  id: null,
  supplierName: '',
  linkName: '',
  linkPhone: '',
  provinceId: null,
  cityCode: null,
  address: ''
})

// 表单验证规则
const rules = {
  supplierName: [
    { required: true, message: '请输入供应商名字', trigger: 'blur' }
  ],
  linkName: [
    { required: true, message: '请输入联系人名字', trigger: 'blur' }
  ],
  linkPhone: [
    { required: true, message: '请输入联系人手机', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur' }
  ],
  provinceId: [
    { required: true, message: '请选择省份', trigger: 'change' }
  ],
  cityCode: [
    { required: true, message: '请选择城市', trigger: 'change' }
  ],
  address: [
    { required: true, message: '请输入详细地址', trigger: 'blur' }
  ]
}

const formRef = ref(null)

// 获取表格数据
const getTableData = () => {
  loading.value = true
  // 模拟API调用
  setTimeout(() => {
    // 模拟数据
    tableData.value = [
      {
        id: 1,
        supplierName: '阿里巴巴集团',
        linkName: '张三',
        linkPhone: '13800138000',
        provinceId: 330000,
        cityCode: 330100,
        address: '杭州市西湖区文一西路969号'
      },
      {
        id: 2,
        supplierName: '腾讯科技',
        linkName: '李四',
        linkPhone: '13900139000',
        provinceId: 440000,
        cityCode: 440300,
        address: '深圳市南山区科技园'
      }
    ]
    total.value = 2
    loading.value = false
  }, 500)
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

// 重置搜索
const handleReset = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = ''
  })
  handleSearch()
}

// 新增
const handleAdd = () => {
  dialogTitle.value = '新增供应商'
  isEdit.value = false
  resetForm()
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  dialogTitle.value = '编辑供应商'
  isEdit.value = true
  Object.assign(form, row)
  dialogVisible.value = true
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    `确定要删除供应商"${row.supplierName}"吗？`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    // 模拟删除API调用
    ElMessage.success('删除成功')
    getTableData()
  }).catch(() => {
    // 用户取消删除
  })
}

// 分页变化
const handlePageChange = (page) => {
  currentPage.value = page
  getTableData()
}

// 提交表单
const handleSubmit = () => {
  formRef.value.validate((valid) => {
    if (valid) {
      // 模拟保存API调用
      ElMessage.success(isEdit.value ? '编辑成功' : '新增成功')
      dialogVisible.value = false
      getTableData()
    }
  })
}

// 关闭弹窗
const handleDialogClose = () => {
  resetForm()
  formRef.value.resetFields()
}

// 重置表单
const resetForm = () => {
  Object.keys(form).forEach(key => {
    form[key] = ''
  })
}

// 初始化数据
onMounted(() => {
  getTableData()
})
</script>

<style scoped>
.supplier-container {
  padding: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.toolbar {
  margin-bottom: 20px;
  text-align: right;
}

.dialog-footer {
  text-align: right;
}
</style>